package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Car;
import cn.wolfcode.crm.mapper.CarMapper;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CarServiceImpl implements ICarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private ICustomerService customerService;
    @Override
    public List<Car> list() {
        return carMapper.selectSomeInfo();
    }
    @Override
    public void insert(Car car) {
        carMapper.insert(car);
    }
    @Override
    public void updateByPrimaryKey(Car car) {
        Long customerId = car.getCustomer().getId();
        String customerName = car.getCustomer().getName();
        //更新客户的信息
        customerService.updateByName(customerId, customerName);
        carMapper.updateByPrimaryKey(car);
    }

    @Override
    public Car selectByPrimaryKey(Long id) {
        Car car = carMapper.selectByPrimaryKey(id);
        return car;
    }

    public PageResult query(QueryObject qo) {
        int totalCount = carMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult();
        }
        List<Car> list = carMapper.queryForList(qo);
        return new PageResult(totalCount, list);
    }
}
